package com.yanqu.road.server.gameplayer.module.activity.mall;

import com.yanqu.road.dao.impl.activity.mall.UserActivityMallTouchDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.mall.ActivityMallTouchConfig;
import com.yanqu.road.entity.activity.mall.UserActivityMallTouchData;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.mall.ActivityRmbMallTouchMgr;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserActivityMallTouchModule extends GeneralModule {

    public Map<Integer, Map<Integer, UserActivityMallTouchData>> activityMallDataMap = new ConcurrentHashMap<>();

    public UserActivityMallTouchModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        List<Integer> list = new ArrayList<>(ActivityRmbMallTouchMgr.getRmbMallInfoTypeMap().keySet());
        if(list.isEmpty()){
            return true;
        }
        List<Integer> showList = new ArrayList<>();
        for (int activityId : list) {
            ActivityInfo openActivityInfo = ActivityMgr.getOpenActivityInfo(activityId);
            if(!ActivityMgr.activityInShowTime(openActivityInfo)){
                continue;
            }
            showList.add(activityId);
        }
        activityMallDataMap = new UserActivityMallTouchDataDaoImpl().getUserActivityMallTouchData(showList);
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        for (Map.Entry<Integer, Map<Integer, UserActivityMallTouchData>> entry : activityMallDataMap.entrySet()) {
            for (Map.Entry<Integer, UserActivityMallTouchData> dataEntry : entry.getValue().entrySet()) {
                UserActivityMallTouchData data = dataEntry.getValue();
                if(data.isInsertOption()){
                    new UserActivityMallTouchDataDaoImpl().add(data);
                }else if(data.isUpdateOption()){
                    new UserActivityMallTouchDataDaoImpl().update(data);
                }
            }
        }
        return true;
    }

    /**
     * 减少礼包数量
     * @param activityId
     * @param mallId
     */
    public synchronized void reTriggerMallNum(int activityId, int mallId) {
        //判断玩家是否存在
        Map<Integer, UserActivityMallTouchData> dataMap = activityMallDataMap.get(activityId);
        if (activityMallDataMap == null) {
            return;
        }
        UserActivityMallTouchData userMallData = dataMap.get(mallId);
        if (userMallData == null) {
            return;
        }
        int newNum = Math.max(userMallData.getNum() - 1, 0);
        userMallData.setNum(newNum);
        //同步玩家信息
        this.syncUserData(activityId);
    }

    /**
     * 添加触发礼包
     * @param activityId
     */
    public synchronized void addTriggerMall(int activityId) {
        //判断礼包是否存在
        if(!activityMallDataMap.containsKey(activityId)){
            activityMallDataMap.put(activityId, new ConcurrentHashMap<>());
        }
        //配置
        ActivityMallTouchConfig config = ActivityRmbMallTouchMgr.getConfig(activityId);
        if (config == null) {
            return;
        }
        //获取触发礼包
        int vipLevel = VipMgr.getVipLv(player.getUserInfo().getVipExp());
        int mallId = ActivityRmbMallTouchMgr.randomTriggerMallId(activityId, vipLevel);
        if (mallId <= 0) {
            return;
        }
        //玩家注入触发礼包
        long now = System.currentTimeMillis();
        long newEffectTime = now + config.getCOMMON_MALL_KEEP_TIME() * 1000L;
        UserActivityMallTouchData userMallData = activityMallDataMap.get(activityId).get(mallId);
        if (userMallData == null) {
            userMallData = new UserActivityMallTouchData(activityId, mallId, 1, newEffectTime);
            userMallData.setInsertOption();
            activityMallDataMap.get(activityId).put(mallId, userMallData);
        } else {
            if(now > userMallData.getEffectTime()){
                userMallData.setNum(1);
            }else {
                userMallData.setNum(userMallData.getNum() + 1);
            }
            userMallData.setEffectTime(newEffectTime);
        }
        //同步一下区服玩家数据
        this.syncUserData(activityId);
    }

    private void syncUserData(int activityId) {
        Map<Integer, UserActivityMallTouchData> touchDataMap = activityMallDataMap.get(activityId);
        ActivityProto.ActivityTouchMallDataSync.Builder clientMsg = ActivityProto.ActivityTouchMallDataSync.newBuilder();
        if(touchDataMap != null){
            ActivityProto.ActivityTouchMallData.Builder activityBuilder = ActivityProto.ActivityTouchMallData.newBuilder();
            activityBuilder.setActivityId(activityId);
            for (Map.Entry<Integer, UserActivityMallTouchData> entry : touchDataMap.entrySet()) {
                UserActivityMallTouchData touchData = entry.getValue();
                ActivityProto.TouchMallData.Builder builder = ActivityProto.TouchMallData.newBuilder();
                builder.setNum(touchData.getNum());
                builder.setMallId(touchData.getMallId());
                builder.setEffectTime(touchData.getEffectTime());
                activityBuilder.addMall(builder);
            }
            clientMsg.addMallData(activityBuilder);
        }
        player.sendPacket(Protocol.U_ACTIVITY_MALL_TOUCH_SYNC, clientMsg);
    }
}
